// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import axios from 'axios'
import router from './router'
import { Message, Col, Select, Option, OptionGroup, Upload, Form, MessageBox, FormItem, Input, Button, Menu, MenuItem, Dialog, Table, TableColumn, Pagination, Loading, Row, Radio, RadioGroup } from 'element-ui'

Vue.use(Form).use(Pagination).use(Loading).use(Row).use(Radio).use(RadioGroup)
Vue.use(FormItem)
Vue.use(Input)
Vue.use(Button)
Vue.use(Menu)
Vue.use(MenuItem)
Vue.use(Table)
Vue.use(TableColumn)
Vue.use(Dialog)
Vue.use(Select)
Vue.use(Option)
Vue.use(OptionGroup)
Vue.use(Upload)
Vue.use(Col)

Vue.prototype.$msgbox = MessageBox
Vue.prototype.$alert = MessageBox.alert
Vue.prototype.$confirm = MessageBox.confirm
Vue.prototype.$prompt = MessageBox.prompt
Vue.prototype.$message = Message

axios.defaults.baseURL = process.env.BASE_API
// axios.defaults.headers.common['token'] = localStorage.getItem('token')
/**
* 请求拦截器，请求发送之前做些事情
*/
axios.interceptors.request.use(
  config => {
    // 配置token参数携带用户token
    let nowTime = Number.parseInt(Date.now() / 1000)
    let newToken = localStorage.getItem('token')
    // console.log(newToken)
    if (newToken && (nowTime < localStorage.getItem('expire') + localStorage.getItem('token_time'))) {
      config.headers.token = newToken
    } else {
      router.replace({
        path: '/'
      })
    }
    return config
  },
  error => {
    localStorage.clear('token')
    router.replace({
      path: '/'
    })
    return Promise.reject(error)
  }
)
// http response 拦截器
axios.interceptors.response.use(
  response => {
    if (response.headers.token) {
      let nowTime = Number.parseInt(Date.now() / 1000)
      if (nowTime < localStorage.getItem('expire') + localStorage.getItem('token_time')) {
        localStorage.setItem('token', response.headers.token)
      }
    }
    if (response.data.errno === 0) {
    } else {
      Message.error({message: response.data.errmsg || '发生错误'})
      if (response.data.errno === 10401) {
        Message.error({message: 'token已过期，请尝试重新登陆'})
        localStorage.clear()
        router.replace({
          path: '/'
        })
      } else {
        // switch (response.data.errno) {
        //   case 401:
        //     Message.error({message: response.data.errmsg})
        //     localStorage.clear()
        //     router.replace({
        //       path: '/',
        //       query: { redirect: router.currentRoute.path }
        //     })
        //     break
        //   case 403:
        //     Message.error({message: (response.data.errmsg ? response.data.errmsg : '操作被拒绝')})
        //     break
        //   case 404:
        //     Message.error({message: (response.data.errmsg ? response.data.errmsg : '页面不存在')})
        //     break
        //   default:
        //     Message.error({message: (response.data.errmsg ? response.data.errmsg : '出错啦')})
        // }
      }
    }
    return response
  },
  error => {
    Message.error({message: 'token已过期 | 账号或密码错误，请尝试重新登陆'})
    localStorage.clear()
    router.replace({
      path: '/'
    })
    return Promise.reject(error)
  }
)
// Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
